import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:wallpaper/components/images/BlinkingPlaceholder.dart';
import 'package:wallpaper/components/images/CustomImageCacheManager.dart';
import 'package:wallpaper/components/placeholder/error_eyes.dart';

class ImageLoad extends StatefulWidget {
  final String imageUrl;
  final BoxFit fit;
  final double size;

  const ImageLoad({
    super.key,
    required this.imageUrl,
    this.fit = BoxFit.cover,
    this.size = 30,
  });

  @override
  State<ImageLoad> createState() => _ImageLoadState();
}

class _ImageLoadState extends State<ImageLoad>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    // 初始化动画控制器
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 600),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: widget.imageUrl,
      memCacheWidth: 500, // 限制内存缓存宽度
      fit: BoxFit.cover,
      cacheManager: CustomImageCacheManager.instance.cacheManager,
      filterQuality: FilterQuality.low,
      errorWidget: (context, url, error) => ErrorEyes(),
      progressIndicatorBuilder: (context, url, downloadProgress) {
        return const BlinkingPlaceholder();
      },
    );
  }
}
